package com.ochnas.volunteersignup.domain;

import org.dom4j.*;
import org.hibernate.dialect.IngresDialect;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Transient;

@Embeddable
public class Time implements Serializable {
	
	@Transient
	private static SimpleDateFormat simpleTimeFormat = new SimpleDateFormat("hh:mm a");
	@Transient
	private static Calendar calendar = Calendar.getInstance();

	/**
	 * hourOfDay ranges from 0 to 23, where midnight is represented by 0.
	 */
	@Column(name = "TIME_HOUR_OF_DAY", nullable = false)
	private int hourOfDay;
	
	/**
	 * minutes ranges from 0 to 59.
	 */
	@Column(name = "TIME_MINUTES", nullable = false)
	private int minutes;
	
	/**
	 * No-argument constructor.
	 */
	public Time() {
		super();
	}
	
	/**
	 * Time-defined constructor.
	 * @param hourOfDay
	 * @param minutes
	 */
	public Time(int hourOfDay, int minutes) {
		super();
		this.hourOfDay = hourOfDay;
		this.minutes = minutes;
	}

	/**
	 * Method to return hour of the day
	 * @return int
	 */
	public int getHourOfDay() {
		return hourOfDay;
	}
	
	/**
	 * Method to set hour of the day
	 * @param hourOfDay
	 */
	public void setHourOfDay(int hourOfDay) {
		this.hourOfDay = hourOfDay;
	}
	
	/**
	 * Method to return minutes of the hour
	 * @return int
	 */
	public int getMinutes() {
		return minutes;
	}
	
	/**
	 * Method to set minutes of the hour
	 * @param minutes
	 */
	public void setMinutes(int minutes) {
		this.minutes = minutes;
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj || !(obj instanceof Time)) return false;
		Time tOb = (Time) obj;
		return (tOb.getHourOfDay() == getHourOfDay() &&
				tOb.getMinutes() == getMinutes());
	}

	@Override
	public String toString() {
		calendar.set(Calendar.HOUR_OF_DAY, getHourOfDay());
		calendar.set(Calendar.MINUTE, getMinutes());
		Date dateTime = calendar.getTime();
		return simpleTimeFormat.format(dateTime);
	}
	
	public String toXml() {
		Document response = DocumentFactory.getInstance().createDocument();
		Element timeElement = response.addElement("time");
		
		Element hdElement = timeElement.addElement("hourOfDay");
		hdElement.addText(String.valueOf(getHourOfDay()));
				
		Element mElement = timeElement.addElement("minutes");
		mElement.addText(String.valueOf(getMinutes()));
		
		return response.asXML();
	}
	
	public static Time fromXml(String xml) {
		try {
			Document doc = DocumentHelper.parseText(xml);
			Node hourOfDayNode = doc.selectSingleNode("/time/hourOfDay");
			String hourOfDay = hourOfDayNode.getText();
			
			Node minutesNode = doc.selectSingleNode("/time/minutes");
			String minutes = minutesNode.getText();
			
			Time t = new Time(Integer.parseInt(hourOfDay), Integer.parseInt(minutes));
			return t;
		} catch (DocumentException x) {
			throw new RuntimeException("Invalid time xml: " + xml, x);
		}
	}
	
}
